package api

import (
	"errors"
	"github.com/gin-gonic/gin"
	"go-caipu/pkg/api/helper"
	"go-caipu/pkg/constant"
	"go-caipu/pkg/services/admin/dept"
)

// GetDeptTreeList 部门管理使用
func (hs *HTTPServer) GetDeptTreeList(c *gin.Context) {
	cmd := dept.GetDeptTreeListCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, err := hs.deptService.GetDeptTreeList(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)
}

// GetDeptTree 部门选择菜单使用（后面优化成前端生成树功能）
func (hs *HTTPServer) GetDeptTree(c *gin.Context) {
	cmd := dept.GetDeptTreeListCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, err := hs.deptService.GetDeptTree(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)
}

// GetDeptOptionsHandler 获取插件列表选项
func (hs *HTTPServer) GetDeptOptionsHandler(c *gin.Context) {
	result, err := hs.deptService.GetDeptSelect(c.Request.Context())
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}

	helper.SuccessWithData(c, result)
}
func (hs *HTTPServer) GetDept(c *gin.Context) {
	cmd := dept.GetDeptCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, err := hs.deptService.Get(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)
}

func (hs *HTTPServer) CreateDept(c *gin.Context) {
	cmd := dept.Dept{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.deptService.Insert(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) UpdateDept(c *gin.Context) {
	cmd := dept.Dept{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.deptService.Update(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) RemoveDept(c *gin.Context) {
	cmd := dept.DeleteDeptCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.deptService.Remove(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}
func (hs *HTTPServer) RemoveDeptByIds(c *gin.Context) {
	id := c.Param("id")
	if id == "" {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, errors.New("id 不能为空"))
		return
	}
	cmd := dept.DeleteDeptCommand{}
	cmd.Ids = []string{id}
	err := hs.deptService.Remove(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}
